package com.ruoyi.web.controller.okr;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IOkrCycleService;
import com.ruoyi.system.service.IOkrInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IOkrTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务管理Controller
 * 
 * @author ruoyi
 * @date 2021-09-19
 */
@Controller
@RequestMapping("/system/task")
public class OkrTaskController extends BaseController
{
    private String prefix = "system/task";

    @Autowired
    private IOkrTaskService okrTaskService;

    @Autowired
    private IOkrInfoService okrInfoService;

    @RequiresPermissions("system:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询任务管理列表
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OkrTask okrTask)
    {
        startPage();
        okrTask.setUserId(ShiroUtils.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        return getDataTable(list);
    }

    /**
     * 导出任务管理列表
     */
    @RequiresPermissions("system:task:export")
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OkrTask okrTask)
    {
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        ExcelUtil<OkrTask> util = new ExcelUtil<OkrTask>(OkrTask.class);
        return util.exportExcel(list, "任务管理数据");
    }

    /**
     * 新增任务管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存任务管理
     */
    @RequiresPermissions("system:task:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OkrTask okrTask)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        okrTask.setUserId(sysUser.getUserId());
        okrTask.setCreatTime(DateUtils.getNowDate());

        return toAjax(okrTaskService.insertOkrTask(okrTask));
    }

    /**
     * 修改任务管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OkrTask okrTask = okrTaskService.selectOkrTaskById(id);
        mmap.put("okrTask", okrTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务管理
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OkrTask okrTask)
    {
        return toAjax(okrTaskService.updateOkrTask(okrTask));
    }

    /**
     * 删除任务管理
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(okrTaskService.deleteOkrTaskByIds(ids));
    }

    Long OkrId;
    @PostMapping("/getOkrId")
    @ResponseBody
    public AjaxResult getRowId(Long id){
        this.OkrId = id;
        return AjaxResult.success(id);
    }

    @RequestMapping("/getMyTask")
    public String getMyTask(Model model){
        System.out.println(OkrId);
        List<OkrTask> tasks = okrTaskService.selectOkrRoleTaskByRowId(OkrId);
        System.out.println(tasks);
        model.addAttribute("tasks", tasks);
        return "okr/LinkTask";
    }

    /**
     * 目标关联任务
     * @param taskIds
     * @return
     */
    @RequestMapping("linkOkrWithTask")
    @ResponseBody
    public AjaxResult linkOkrWithTask(@RequestParam("taskIds[]")List<Long> taskIds){
        System.out.println(OkrId);
        okrTaskService.deleteOkrRoleTask(OkrId);
        if (taskIds.get(0).equals(-1L)){
            return AjaxResult.success("取消关联成功");
        }
        for (Long id : taskIds){
            okrTaskService.insertOkrRoleTask(OkrId, id);
        }
        return AjaxResult.success("关联成功");
    }


    @GetMapping("/taskKanban")
    public String getKanban(Model model){
        SysUser user = ShiroUtils.getSysUser();
        OkrTask okrTask = new OkrTask();
        okrTask.setUserId(user.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        model.addAttribute("tasks", list);
        return "/okr/taskKanban";
    }


    @RequestMapping("/updateStatus")
    @ResponseBody
    public AjaxResult getStatus(Long id, Integer status){
        OkrTask okrTask = okrTaskService.selectOkrTaskById(id);
        okrTask.setStatus(status);
        okrTaskService.updateOkrTask(okrTask);
        return AjaxResult.success("修改状态成功");
    }



    @RequestMapping("/getTaskList")
    @ResponseBody
    public List<OkrTask> getTaskList(){
        List<OkrTask> tasks = okrTaskService.selectOkrRoleTaskByRowId(OkrId);
//        okrTaskService.selectOkrRoleTaskByRowId();
        return tasks;
    }
}
